﻿using System;

namespace com.binance.client
{
	using BinanceApiException = com.binance.client.exception.BinanceApiException;

	/// <summary>
	/// The configuration for the subscription APIs
	/// </summary>
	public class SubscriptionOptions
	{

		private string uri = "wss://api.binance.pro/";
		private bool isAutoReconnect = true;
		private int receiveLimitMs = 60_000;
		private int connectionDelayOnFailure = 15;

		public SubscriptionOptions(SubscriptionOptions options)
		{
			this.uri = options.uri;
			this.isAutoReconnect = options.isAutoReconnect;
			this.receiveLimitMs = options.receiveLimitMs;
			this.connectionDelayOnFailure = options.connectionDelayOnFailure;
		}

		public SubscriptionOptions()
		{
		}

		/// <summary>
		/// Set the URI for subscription.
		/// </summary>
		/// <param name="uri"> The URI name like "wss://api.binance.pro". </param>
		public virtual string Uri
		{
			set
			{
				try
				{
					Uri u = new Uri(value);
					this.uri = u.ToString();
				}
				catch (Exception e)
				{
					throw new BinanceApiException(BinanceApiException.INPUT_ERROR, "The URI is incorrect: " + e.Message);
				}
				this.uri = value;
			}
			get
			{
				return uri;
			}
		}

		/// <summary>
		/// Set the receive limit in millisecond. If no message is received within this
		/// limit time, the connection will be disconnected.
		/// </summary>
		/// <param name="receiveLimitMs"> The receive limit in millisecond. </param>
		public virtual int ReceiveLimitMs
		{
			set
			{
				this.receiveLimitMs = value;
			}
			get
			{
				return receiveLimitMs;
			}
		}

		/// <summary>
		/// If auto reconnect is enabled, specify the delay time before reconnect.
		/// </summary>
		/// <param name="connectionDelayOnFailure"> The delay time in second. </param>
		public virtual int ConnectionDelayOnFailure
		{
			set
			{
				this.connectionDelayOnFailure = value;
			}
			get
			{
				return connectionDelayOnFailure;
			}
		}

		/// <summary>
		/// When the connection lost is happening on the subscription line, specify
		/// whether the client reconnect to server automatically.
		/// <para>
		/// The connection lost means:
		/// <ul>
		/// <li>Caused by network problem</li>
		/// <li>The connection close triggered by server (happened every 24 hours)</li>
		/// <li>No any message can be received from server within a specified time, see
		/// <seealso cref="setReceiveLimitMs(int)"/> (int)}</li>
		/// </ul>
		/// 
		/// </para>
		/// </summary>
		/// <param name="isAutoReconnect"> The boolean flag, true for enable, false for disable </param>
		/// <returns> Return self for chaining </returns>
		public virtual SubscriptionOptions setAutoReconnect(bool isAutoReconnect)
		{
			this.isAutoReconnect = isAutoReconnect;
			return this;
		}

		public virtual bool AutoReconnect
		{
			get
			{
				return isAutoReconnect;
			}
		}



	}

}